package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;

import java.text.DecimalFormat;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityWageSetBinding;
import flc.ast.utils.Content;
import stark.common.basic.utils.SPUtil;
import stark.common.basic.utils.Str2NumUtil;

public class WageSetActivity extends BaseAc<ActivityWageSetBinding> {

    private double monthWage, hourWage, times1, times2, times3, wage1, wage2, wage3;
    private String format = "%.1f";

    @Override
    protected int onCreate() {
        return R.layout.activity_wage_set;
    }

    @Override
    protected void initView() {
        mDataBinding.ivWageSetBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivSave.setOnClickListener(this);

        monthWage = Str2NumUtil.parse(SPUtil.getString(mContext, Content.monthWage, "2000"));
        hourWage = Str2NumUtil.parse(SPUtil.getString(mContext, Content.hourWage, "10"));
        wage1 = Str2NumUtil.parse(SPUtil.getString(mContext, Content.wageTyp1, "15"));
        wage2 = Str2NumUtil.parse(SPUtil.getString(mContext, Content.wageTyp2, "20"));
        wage3 = Str2NumUtil.parse(SPUtil.getString(mContext, Content.wageTyp3, "30"));
        times1 = Str2NumUtil.parse(SPUtil.getString(mContext, Content.wageTimes1, "1.5"));
        times2 = Str2NumUtil.parse(SPUtil.getString(mContext, Content.wageTimes2, "2"));
        times3 = Str2NumUtil.parse(SPUtil.getString(mContext, Content.wageTimes3, "3"));
        setData();

        //小时工资
        mDataBinding.etHourWage.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                hourWage = Str2NumUtil.parse(s.toString().trim());
                wage1 = hourWage * times1;
                wage2 = hourWage * times2;
                wage3 = hourWage * times3;
                mDataBinding.tvWage1.setText(String.valueOf(hourWage));
                mDataBinding.tvWage2.setText(String.valueOf(hourWage));
                mDataBinding.tvWage3.setText(String.valueOf(hourWage));
                mDataBinding.etWage1.setText(String.valueOf(wage1));
                mDataBinding.etWage2.setText(String.valueOf(wage2));
                mDataBinding.etWage3.setText(String.valueOf(wage3));
            }
        });

        //平时加班
        mDataBinding.etTimes1.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                times1 = Str2NumUtil.parse(s.toString());
                wage1 = hourWage * times1;
                mDataBinding.etWage1.setText(String.valueOf(wage1));
            }
        });
//        mDataBinding.etWage1.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//                wage1 = Str2NumUtil.parse(s.toString());
//                times1 = wage1 / hourWage;
//                mDataBinding.etTimes1.setText(String.valueOf(times1));
//            }
//        });

        //周末加班
        mDataBinding.etTimes2.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                times2 = Str2NumUtil.parse(s.toString());
                wage2 = hourWage * times2;
                mDataBinding.etWage2.setText(String.valueOf(wage2));
            }
        });
//        mDataBinding.etWage2.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//                wage2 = Str2NumUtil.parse(s.toString());
//                times2 = wage2 / hourWage;
//                mDataBinding.etTimes2.setText(String.valueOf(times2));
//            }
//        });

        //节假日加班加班
        mDataBinding.etTimes3.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                times3 = Str2NumUtil.parse(s.toString());
                wage3 = hourWage * times3;
                mDataBinding.etWage3.setText(String.valueOf(wage3));
            }
        });
//        mDataBinding.etWage3.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//                wage3 = Str2NumUtil.parse(s.toString());
//                times3 = wage3 / hourWage;
//                mDataBinding.etTimes3.setText(String.valueOf(times3));
//            }
//        });
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivSave://保存
                SPUtil.putString(mContext, Content.monthWage, String.valueOf(monthWage));
                SPUtil.putString(mContext, Content.hourWage, String.valueOf(hourWage));
                SPUtil.putString(mContext, Content.wageTyp1, String.format(format, wage1));
                SPUtil.putString(mContext, Content.wageTyp2, String.format(format, wage2));
                SPUtil.putString(mContext, Content.wageTyp3, String.format(format, wage3));
                SPUtil.putString(mContext, Content.wageTimes1, String.format(format,times1));
                SPUtil.putString(mContext, Content.wageTimes2, String.format(format,times2));
                SPUtil.putString(mContext, Content.wageTimes3, String.format(format,times3));
                ToastUtils.showShort(getString(R.string.save_success));
                Intent intent = new Intent();
                setResult(RESULT_OK, intent);
                finish();
                break;
        }
    }

    @Override
    protected void initData() {

    }

    private void setData() {
        mDataBinding.etMonthWage.setText(String.valueOf(monthWage));
        mDataBinding.etHourWage.setText(String.valueOf(hourWage));
        mDataBinding.tvWage1.setText(String.valueOf(hourWage));
        mDataBinding.tvWage2.setText(String.valueOf(hourWage));
        mDataBinding.tvWage3.setText(String.valueOf(hourWage));
        mDataBinding.etTimes1.setText(String.valueOf(times1));
        mDataBinding.etTimes2.setText(String.valueOf(times2));
        mDataBinding.etTimes3.setText(String.valueOf(times3));
        mDataBinding.etWage1.setText(String.valueOf(wage1));
        mDataBinding.etWage2.setText(String.valueOf(wage2));
        mDataBinding.etWage3.setText(String.valueOf(wage3));
    }

}